package bTree.LeetCode;

import java.util.Arrays;
import java.util.Comparator;

public class 插入区间 {
    public int[][] insert(int[][] intervals, int[] newInterval) {
        int row=intervals.length;
        int [][]target=new int[row+1][2];
        for (int i = 0; i < row; i++) {
            target[i]=intervals[i];
        }
        target[row]=newInterval;
        Arrays.sort(target,((o1, o2) -> o1[0]-o2[0]));
        int [][]target2=new int[row+1][2];
        int begin=target[0][0];
        int end=target[0][1];
        int cnt=0;
        for (int i=1;i<target.length;i++){
            if (target[i][0]>end){
                target2[cnt][0]=begin;
                target2[cnt++][1]=end;
                begin=target[i][0];
                end=target[i][1];
            }
            if(target[i][0]<end&&target[i][1]>end){
                end=target[i][1];
            }
            if(target[i][0]==end){
                end=target[i][1];
            }
        }
        target2[cnt][0]=begin;
        target2[cnt][1]=end;
        return Arrays.copyOfRange(target2,0,cnt+1);
    }
}